package com.share.example.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.utils.DateUtil;
import com.share.example.stock.mapper.SectorSpotMapper;
import com.share.example.stock.model.db.SectorSpot;
import com.share.example.stock.service.AkService;
import com.share.example.stock.service.ISectorSpotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-06-03
 */
@Slf4j
@Service
public class SectorSpotServiceImpl extends ServiceImpl<SectorSpotMapper, SectorSpot> implements ISectorSpotService {
    @Resource
    private SectorSpotMapper sectorSpotMapper;
    @Autowired
    private AkService akHttpService;
    private Map<String, SectorSpot> lableMap = new HashMap<>();

    @Override
    public List<SectorSpot> downSector(String indicator) {
        //1.获取接口数据
        String strCurDate = DateUtil.getDate();
        List<SectorSpot> dataList = akHttpService.stock_sector_spot(indicator, strCurDate);
        log.info("拉取indicator={},条数={}", indicator, dataList.size());
        if (StringUtils.isEmpty(dataList)) {
            return dataList;
        }

        //4.数据入库
        if (dataList.size() > 0) {
            int countd = sectorSpotMapper.deleteByDateAndIndicator(strCurDate, indicator);//一个行业一个行业的删除

            log.info("indicator={},批量插入size={}", indicator, dataList.size());
            int counti = sectorSpotMapper.insertbatch(dataList);
        }

        return dataList;
    }

    @Override
    public List<SectorSpot> initSectorSpot() {
        log.info("正在初始化sectorSpot");
        QueryWrapper<SectorSpot> wrapper = new QueryWrapper();
        wrapper.select("DISTINCT indicator,lable,plate");
        List<SectorSpot> list = sectorSpotMapper.selectList(wrapper);

        Map<String, SectorSpot> tmpMap = new HashMap<>();
        for (SectorSpot item : list) {
            tmpMap.put(item.getLable(), item);
        }

        //追加标签
        if (!CollectionUtils.isEmpty(tmpMap)) {
            lableMap.putAll(tmpMap);
        }
        log.info("正在初始化sectorSpot---完毕");
        return list;
    }

    @Override
    public String getLableName(String lable) {
        SectorSpot sectorSpot = lableMap.get(lable);
        if (sectorSpot == null) {
            return null;
        } else {
            return sectorSpot.getPlate();
        }
    }

    @Override
    public List<SectorSpot> listLables() {
        List<SectorSpot> list = new LinkedList<>();
        for (String key : lableMap.keySet()) {
            list.add(lableMap.get(key));
        }
        return list;
    }
}
